
package com.winning.sx.microframework.controller.busi;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.*;
import com.winning.sx.microframework.controller.base.QRcodeController;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/busi/equipment/useChange")
public class EquipmentUseChangeController {
    @Autowired
    EasyService easyService;
    @Autowired
    ConstService service;
    @Autowired
    RoleManageService roleService;

    @RequestMapping(value = {"/uchange"})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/busi/equipmentUseChangeU");

        List staffList = easyService.querySql("select  *  from BASE_Staff   where  validFlag='100001' and organizationId in (" +
                " select id from BASE_Organization where organizationType='400001' " +
                ") ", new HashMap()).getDataRows();
        mv.addObject("staffList", staffList);

        List changeType = service.queryCount("121");
        mv.addObject("changeType", changeType);
        List staffType = service.queryCount("311");
        mv.addObject("staffType", staffType);
        DataTable all = roleService.queryRoles();
        List staffRole = all.getDataRows();
        mv.addObject("staffRole", staffRole);

        List useType = service.queryCount("108");
        mv.addObject("useType", useType);
        List echangeType = service.queryCount("123");
        mv.addObject("echangeType", echangeType);

        List warehouseList = easyService.querySql("select  *  from BASE_Warehouse  where  validFlag='100001' ", new HashMap()).getDataRows();
        mv.addObject("warehouseList", warehouseList);

        List state = service.queryCount("334");
        mv.addObject("state", state);
        LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
        Map<String, String> param = new HashMap();
        param.put("organizationType", "400001");
        param.put("superOrganizationId", login.getSuperOrgId());
        mv.addObject("organizationName", JSON.toJSON(service.queryMyOrg(param)).toString());

        return mv;
    }
    @RequestMapping(value = {"/batchChange"})
    public ModelAndView binit() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/busi/equipmentUseChangeB");

        Map map = new HashMap();
        List eqClass = easyService.querySql("SELECT " +
                "  c1.name + '-' + c2.name name, " +
                "  c2.code " +
                "FROM BASE_EqClass c1 " +
                "  RIGHT JOIN BASE_EqClass c2 ON c1.id = c2.superId " +
                "WHERE c2.validFlag = '100001' AND c1.id IS NOT NULL ", map).getDataRows();
        mv.addObject("eqClass", eqClass);
        List brand = service.queryCount("300");
        mv.addObject("brand", brand);

        List echangeType = service.queryCount("123");
        mv.addObject("echangeType", echangeType);

        List warehouseList = easyService.querySql("select  *  from BASE_Warehouse  where  validFlag='100001' ", new HashMap()).getDataRows();
        mv.addObject("warehouseList", warehouseList);
        List useType = service.queryCount("108");
        mv.addObject("useType", useType);
        List state = service.queryCount("334");
        mv.addObject("state", state);
        LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
        Map<String, String> param = new HashMap();
        param.put("organizationType", "400001");
        param.put("superOrganizationId", login.getSuperOrgId());
        mv.addObject("organizationName", JSON.toJSON(service.queryMyOrg(param)).toString());
        List staffList = easyService.querySql("select  *  from BASE_Staff   where  validFlag='100001' and organizationId in (" +
                " select id from BASE_Organization where organizationType='400001' " +
                ") ", new HashMap()).getDataRows();
        mv.addObject("staffList", staffList);
        return mv;
    }
    @RequestMapping(value = {"/echange"})
    public ModelAndView init2() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/busi/equipmentUseChangeE");
        List staffList = easyService.querySql("select  *  from BASE_Staff   where  validFlag='100001' and organizationId in (" +
                " select id from BASE_Organization where organizationType='400001' " +
                ") ", new HashMap()).getDataRows();
        mv.addObject("staffList", staffList);

        List changeType = service.queryCount("121");
        mv.addObject("changeType", changeType);
        List staffType = service.queryCount("311");
        mv.addObject("staffType", staffType);
        DataTable all = roleService.queryRoles();
        List staffRole = all.getDataRows();
        mv.addObject("staffRole", staffRole);

        List useType = service.queryCount("108");
        mv.addObject("useType", useType);
        List echangeType = service.queryCount("123");
        mv.addObject("echangeType", echangeType);

        List warehouseList = easyService.querySql("select  *  from BASE_Warehouse  where  validFlag='100001' ", new HashMap()).getDataRows();
        mv.addObject("warehouseList", warehouseList);

        List state = service.queryCount("334");
        mv.addObject("state", state);
        List eState = service.queryCount("203");
        mv.addObject("eState", eState);
        List brand = service.queryCount("300");
        mv.addObject("brand", brand);

        List secrecy = service.queryCount("116");
        mv.addObject("secrecy", secrecy);

        Map eqRootClassP = new HashMap();
        eqRootClassP.put("superId", "0");
        eqRootClassP.put("validFlag", "100001");
        eqRootClassP.put("type", "301001");
        DataTable eqRootClassS = easyService.queryList("BASE_EqClass", eqRootClassP);
        List eqRootClass = eqRootClassS.getDataRows();
        mv.addObject("eqRootClass", eqRootClass);

        LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
        Map<String, String> param = new HashMap();
        param.put("organizationType", "400001");
        param.put("superOrganizationId", login.getSuperOrgId());
        mv.addObject("organizationName", JSON.toJSON(service.queryMyOrg(param)).toString());

        Map code = new HashMap();
        List QRcode = easyService.querySql("select * from BASE_QRcode r where r.id in (select e.QRCode from BASE_Equipment e where e.state in ('203004'))", code).getDataRows();
        mv.addObject("QRcode", QRcode);

        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryUchangeList"})
    public AjaxTableResponseMessage queryUchangeList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map paramNew = new HashMap();
            if ("0".equals(param.get("oneOrganization"))) {
                paramNew.put("organizationId", login.getSuperOrgId());
            } else {
                paramNew.put("organizationId", param.get("oneOrganization"));
            }
            Map orgParam = new HashMap();
            orgParam.put("superOrganizationId", paramNew.get("organizationId").toString());
            orgParam.put("organizationType", "400001");
            String orgs = service.queryMyOrg(orgParam).get("orgs").toString();
            if (orgs.length() == 0) {
                orgs = paramNew.get("organizationId").toString();
            } else {
                orgs += ("," + paramNew.get("organizationId").toString());
            }
            String sql = "select " +
                    "  s.name,  s.telephone,s.staffType,boo.name oldOrgName,bon.name newOrgName,ro.cRoleName oldRoleName,rn.cRoleName newRoleName, " +
                    " sc.* from BUSI_StaffChange sc left join BASE_Staff s on sc.staffId=s.id " +
                    "left join BASE_Organization boo on sc.oldOrg=boo.id " +
                    "left join BASE_Organization bon on sc.newOrg=bon.id " +
                    "left join PM_ROLES ro on ro.cRoleID=sc.oldRole " +
                    "left join PM_ROLES rn on rn.cRoleID=sc.newRole " +
                    "where 1=1 and sc.validFlag=100001 " +
                    (paramNew.get("organizationId") != null ? (" and s.organizationId in (" + orgs + ")") : "") +
                    " order by sc.createTime desc ";
            DataTable all = easyService.querySql(sql, paramNew);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryBchangeList"})
    public AjaxTableResponseMessage queryBchangeList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map paramNew = new HashMap();

            String sql = "select " +
                    "c.*,ed.name,eq.name typeName,c1.name brandName , " +
                    "  (select count(1) from BASE_Equipment e where e.modelId=ed.id and e.validFlag='100001' and e.state in ('203001','203002','203003','203004')) eCount1, " +
                    "  (select count(1) from BASE_Equipment e where e.modelId=ed.id and e.validFlag='100001' and e.state in ('203005')) eCount2" +
                    " from BUSI_BatchChange c left join BASE_EquipmentDetail ed on c.modelId=ed.id " +
                    "left join BASE_EqClass eq on eq.code=ed.type " +
                    "left join BASE_Const c1 on c1.code=ed.brand " +
                    "where c.validFlag='100001' order by c.createTime desc ";
            DataTable all = easyService.querySql(sql, paramNew);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryMessageList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map paramNew = new HashMap();
            if (param.get("changeType") != null && !"0".equals(param.get("changeType"))) {
                String changeType = param.get("changeType").toString().trim();
                paramNew.put("changeType", changeType);
            }
            if (param.get("state") != null && !"0".equals(param.get("state"))) {
                String state = param.get("state").toString().trim();
                paramNew.put("state", state);
            }
            if (param.get("useStaffId") != null && !"0".equals(param.get("useStaffId"))) {
                String useStaffId = param.get("useStaffId").toString().trim();
                paramNew.put("useStaffId", useStaffId);
            }

            if (param.get("useType") != null && !"0".equals(param.get("useType"))) {
                String code = param.get("useType").toString().trim();
                paramNew.put("useType", code);
            }
            if (param.get("brand") != null && !"0".equals(param.get("brand"))) {
                String constType = param.get("brand").toString().trim();
                paramNew.put("brandCode", constType);
            }
            if (param.get("type") != null && !"0".equals(param.get("type"))) {
                String code = param.get("type").toString().trim();
                paramNew.put("oneCode", code);
            }
            if (param.get("oneOrganization") == null || "0".equals(param.get("oneOrganization"))) {
                paramNew.put("organizationId", login.getSuperOrgId());
            } else {
                paramNew.put("organizationId", param.get("oneOrganization"));
            }
            Map orgParam = new HashMap();
            orgParam.put("superOrganizationId", paramNew.get("organizationId").toString());
            orgParam.put("organizationType", "400001");
            String orgs = service.queryMyOrg(orgParam).get("orgs").toString();
            if (orgs.length() == 0) {
                orgs = paramNew.get("organizationId").toString();
            } else {
                orgs += ("," + paramNew.get("organizationId").toString());
            }

            String sql = "select " +
                    (param.get("fId") != null ? (param.get("fId").toString() + " as fId,") : "") +
                    "e.id eqId,e.name,e.buyTime,e.usePosition oldPosition,e.barCode,q.code QrCode,e.externalPhotos,m.image,e.organizationId,e.staffId,e.useType " +
                    ",ec.* " +
                    "from BASE_Equipment e " +
                    "left join BASE_EquipmentDetail m on e.modelId=m.id " +
                    "left join BASE_QRcode q on e.QRCode=q.id " +
                    "left join BUSI_EquipmentChange ec on e.id=ec.eId and ec.validFlag='100001'  " +
                    (param.get("fId") != null ? (" and ec.fId=" + param.get("fId")) : "") +
                    (param.get("source") != null ? (" and ec.source='" + param.get("source") + "'") : "") +
                    " where 1=1 and e.validFlag=100001 " +
                    (paramNew.get("oneCode") != null ? (" and e.oneCode like '" + paramNew.get("oneCode") + "%'") : "") +
                    (paramNew.get("brandCode") != null ? (" and e.brandCode='" + paramNew.get("brandCode") + "'") : "") +
                    (paramNew.get("useType") != null ? (" and eg.useType='" + paramNew.get("useType") + "'") : "") +
                    (paramNew.get("organizationId") != null ? (" and e.organizationId in (" + orgs + ")") : "") +

                    (param.get("useStaffId") != null ? (" and (e.staffId = "+ param.get("useStaffId")+" or ec.newStaff="+ param.get("useStaffId")+" or ec.oldStaff="+ param.get("useStaffId")+")") : "") +
                    " order by ec.createTime";
            DataTable all = easyService.querySql(sql, paramNew);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryEList"})
    public AjaxTableResponseMessage queryEList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map paramNew = new HashMap();
            if (param.get("changeType") != null && !"0".equals(param.get("changeType"))) {
                String changeType = param.get("changeType").toString().trim();
                paramNew.put("changeType", changeType);
            }
            if (param.get("state") != null && !"0".equals(param.get("state"))) {
                String state = param.get("state").toString().trim();
                paramNew.put("state", state);
            }
            if (param.get("useStaffId") != null && !"0".equals(param.get("useStaffId"))) {
                String useStaffId = param.get("useStaffId").toString().trim();
                paramNew.put("useStaffId", useStaffId);
            }

            if (param.get("useType") != null && !"0".equals(param.get("useType"))) {
                String code = param.get("useType").toString().trim();
                paramNew.put("useType", code);
            }
            if (param.get("name") != null && !"".equals(param.get("name"))) {
                String code = param.get("name").toString().trim();
                paramNew.put("name", code);
            }
            if (param.get("brand") != null && !"0".equals(param.get("brand"))) {
                String constType = param.get("brand").toString().trim();
                paramNew.put("brandCode", constType);
            }
            if (param.get("type") != null && !"0".equals(param.get("type"))) {
                String code = param.get("type").toString().trim();
                paramNew.put("oneCode", code);
            }
            if (param.get("oneOrganization") == null || "0".equals(param.get("oneOrganization"))) {
                paramNew.put("organizationId", login.getSuperOrgId());
            } else {
                paramNew.put("organizationId", param.get("oneOrganization"));
            }
            Map orgParam = new HashMap();
            orgParam.put("superOrganizationId", paramNew.get("organizationId").toString());
            orgParam.put("organizationType", "400001");
            String orgs = service.queryMyOrg(orgParam).get("orgs").toString();
            if (orgs.length() == 0) {
                orgs = paramNew.get("organizationId").toString();
            } else {
                orgs += ("," + paramNew.get("organizationId").toString());
            }

            String sql = "select " +
                    (param.get("fId") != null ? (param.get("fId").toString() + " as fId,") : "") +
                    "e.id eqId,e.name,e.buyTime,e.usePosition oldPosition,e.barCode,q.code QrCode,e.externalPhotos,e.nameplatePhoto,m.image,e.organizationId,e.staffId,e.useType " +
                    ",ec.* " +
                    "from BASE_Equipment e " +
                    "left join BASE_EquipmentDetail m on e.modelId=m.id " +
                    "left join BASE_QRcode q on e.QRCode=q.id " +
                    "right join BUSI_EquipmentChange ec on e.id=ec.eId and ec.validFlag='100001'  " +
                    (param.get("fId") != null ? (" and ec.fId=" + param.get("fId")) : "") +
                    " where ec.validFlag=100001 " +
                    (param.get("source") != null ? (" and ec.source='" + param.get("source") + "'") : "") +
                    (paramNew.get("oneCode") != null ? (" and e.oneCode like '" + paramNew.get("oneCode") + "%'") : "") +
                    (paramNew.get("brandCode") != null ? (" and e.brandCode='" + paramNew.get("brandCode") + "'") : "") +

                    (paramNew.get("changeType") != null ? (" and ec.type='" + paramNew.get("changeType") + "'") : "") +
                    (paramNew.get("state") != null ? (" and ec.state='" + paramNew.get("state") + "'") : "") +

                    (paramNew.get("organizationId") != null ? (" and (ec.oldOrg in (" + orgs + ") or ec.newOrg in (" + orgs + "))") : "") +
                    (paramNew.get("useType") != null ? (" and (ec.oldUseType =" + paramNew.get("useType")) + " or ec.newUseType =" + paramNew.get("useType") + ")" : "") +
                    (paramNew.get("useStaffId") != null ? (" and (ec.oldStaff =" + paramNew.get("useStaffId")) + " or ec.newStaff =" + paramNew.get("useStaffId") + ")" : "") +

                    (param.get("b_createTime") != null ? (" and ec.createTime>='" + param.get("b_createTime") + " 00:00:00'") : "") +
                    (param.get("e_createTime") != null ? (" and ec.createTime<='" + param.get("e_createTime") + " 23:59:59'") : "") +

                    (paramNew.get("name") != null ? (" and (e.name like '%" + paramNew.get("name") + "%' or q.code like '" + paramNew.get("name") + "' or e.barCode like '" + paramNew.get("name") + "%' ))") : "") +
                    " order by ec.createTime desc";
            DataTable all = easyService.querySql(sql, paramNew);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/checkEChange"})
    @Transactional(propagation = Propagation.REQUIRED)
    public AjaxDataResponseMessage checkEChange(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                Map cparam = new HashMap();
                cparam.put("id", map.get("id"));
                Map eChange = easyService.queryEntity("BUSI_EquipmentChange", cparam);
                eChange.put("state", map.get("state"));
                eChange.put("remark", map.get("reason"));
                eChange.put("checkTime", SqlTool.getNowTimeString());
                easyService.ChangeBeanToStringMap(eChange);
                easyService.save("BUSI_EquipmentChange", "id", eChange);
                if ("334002".equals(map.get("state"))) {
                    Map eparam = new HashMap();
                    eparam.put("id", eChange.get("eId").toString());
                    if ("123001,123002,123009".indexOf(eChange.get("type").toString()) >= 0) {
                        eparam.put("state", "203001");
                        eparam.put("organizationId", eChange.get("newOrg"));
                        eparam.put("usePosition", eChange.get("newPosition"));
                        eparam.put("staffId", eChange.get("newStaff"));
                        eparam.put("useType", eChange.get("newUseType"));
                    } else if ("123003,123004".indexOf(eChange.get("type").toString()) >= 0) {
                        eparam.put("state", "203004");
                        eparam.put("organizationId", eChange.get("newOrg"));
                        eparam.put("usePosition", eChange.get("newPosition"));
                        eparam.put("staffId", eChange.get("newStaff"));
                        eparam.put("useType", eChange.get("newUseType"));
                    } else if ("123005".indexOf(eChange.get("type").toString()) >= 0) {
                        eparam.put("state", "203005");
                    }
                    eparam.put("updateStaffId",login.getStaffId());
                    easyService.ChangeBeanToStringMap(eparam);
                    easyService.save("BASE_Equipment", "id", eparam);
                }

                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/saveUChange"})
    @Transactional(propagation = Propagation.REQUIRED)
    public AjaxDataResponseMessage saveUChange(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                if ("".equals(map.get("id").toString())) {
                    map.put("createStaffId", login.getStaffId().toString());
                    map.put("flowCode", FlowCodeUtil.getFlowCode());
                    map.put("state", "334001");
                } else map.put("updateStaffId", login.getStaffId().toString());
                easyService.save("BUSI_StaffChange", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ajaxDataResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/saveBChange"})
    @Transactional(propagation = Propagation.REQUIRED)
    public AjaxDataResponseMessage saveBChange(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                Map param=new HashMap();
                if ("".equals(map.get("id").toString())) {
                    param.put("createStaffId", login.getStaffId().toString());
                } else {
                    param.put("id",map.get("id").toString());
                    param.put("updateStaffId", login.getStaffId().toString());
                }
                param.put("reason", map.get("reason").toString());
                param.put("modelId", map.get("modelId").toString());
                easyService.save("BUSI_BatchChange", "id", param);

                Map queryModelE=new HashMap();
                queryModelE.put("modelId",map.get("modelId").toString());
                List eList=easyService.querySql("select * from BASE_Equipment where modelId=:modelId and validFlag='100001' and state in ('203001','203002','203003','203004')",queryModelE).getDataRows();
                for(Map entity :(List<Map>)eList){
                    Map change =new HashMap();
                    change.put("fId",param.get("id").toString());
                    change.put("type","123005");
                    change.put("eId",entity.get("id").toString());
                    change.put("oldUseType",entity.get("useType"));
                    if(entity.get("organizationId")!=null) change.put("oldOrg",entity.get("organizationId").toString());
                    if(entity.get("staffId")!=null) change.put("oldStaff",entity.get("staffId").toString());
                    change.put("oldPosition",entity.get("usePosition"));
                    change.put("source","124004");
                    change.put("state","334001");
                    change.put("warehouseId",map.get("warehouseId").toString());
                    easyService.save("BUSI_EquipmentChange","id",change);
                }
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ajaxDataResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/saveEChange"})
    @Transactional(propagation = Propagation.REQUIRED)
    public AjaxDataResponseMessage saveEChange(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            LoginSession login = (LoginSession) (SecurityUtils.getSubject().getSession().getAttribute("loginSession"));
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                if ("".equals(map.get("id").toString())) {
                    map.put("createStaffId", login.getStaffId().toString());
                    map.put("flowCode", FlowCodeUtil.getFlowCode());
                    map.put("state", "334001");
                } else map.put("updateStaffId", login.getStaffId().toString());
                if ("123004,123005".indexOf(map.get("type").toString()) >= 0) {
                    map.put("newUseType", "null");
                    map.put("newOrg", "null");
                    map.put("newStaff", "null");
                    map.put("newPosition", "null");
                } else {
                    map.put("warehouseId", "null");
                }
                easyService.save("BUSI_EquipmentChange", "id", map);

                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ajaxDataResponseMessage;
    }


    @ResponseBody
    @RequestMapping(value = {"/deleteU"})
    public AjaxDataResponseMessage deleteU(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BUSI_StaffChange", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BUSI_EquipmentChange", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/print/{id}"})
    public ModelAndView print(@PathVariable("id") String id) throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/busi/printEquipmentUseChange");
        Map map = new HashMap();
        ObjectMapper objectMapper = new ObjectMapper();
        //获取主要数据
        Map mainData = easyService.querySql("select " +
                "  s.*,boo.name oldOrgName,bon.name newOrgName,ro.cRoleName oldRoleName,rn.cRoleName newRoleName, " +
                " c1.name changeTypeName,c2.name staffTypeName,c3.name newTypeName,c4.name oldTypeName," +
                " sc.* from BUSI_StaffChange sc left join BASE_Staff s on sc.staffId=s.id " +
                "left join BASE_Organization boo on sc.oldOrg=boo.id " +
                "left join BASE_Organization bon on sc.newOrg=bon.id " +
                "left join PM_ROLES ro on ro.cRoleID=sc.oldRole " +
                "left join PM_ROLES rn on rn.cRoleID=sc.newRole " +
                "left join base_const c1 on c1.code=sc.type " +
                "left join base_const c3 on c3.code=sc.newType " +
                "left join base_const c4 on c4.code=sc.oldType " +
                "left join base_const c2 on c2.code=s.staffType " +
                " where sc.id=" + id, map).getDataRows().get(0);


        List detailList = easyService.querySql("select " +
                "h.name warehouseName," +
                "e.id eqId,e.name,e.buyTime,e.usePosition oldPosition,e.barCode,q.code QrCode,ISNULL(e.externalPhotos,m.image) as externalPhotos," +
                "e.organizationId,e.staffId,e.useType ," +
                "o1.name nowOrgName,o2.name oldOrgName,o3.name newOrgName, " +
                "c1.name nowUseTypeName,c2.name oldUseTypeName,c3.name newUseTypeName, " +
                "s1.name nowStaffName,s2.name oldStaffName,s3.name newStaffName, " +
                "ec.* " +
                "from " +
                " BUSI_EquipmentChange ec  " +
                " left join BASE_Equipment e on e.id=ec.eId " +
                "left join BASE_EquipmentDetail m on e.modelId=m.id " +
                "left join BASE_QRcode q on e.QRCode=q.id " +
                "left join BASE_Warehouse h on h.id=ec.warehouseId " +

                "left join BASE_Organization o1 on o1.id=e.organizationId " +
                "left join BASE_Organization o2 on o2.id=ec.oldOrg " +
                "left join BASE_Organization o3 on o3.id=ec.newOrg " +

                "left join BASE_Const c1 on c1.code=e.useType " +
                "left join BASE_Const c2 on c2.code=ec.oldUseType " +
                "left join BASE_Const c3 on c3.code=ec.newUseType " +

                "left join BASE_staff s1 on s1.id=e.staffId " +
                "left join BASE_staff s2 on s2.id=ec.oldStaff " +
                "left join BASE_staff s3 on s3.id=ec.newStaff " +

                " where ec.validFlag='100001'  " +
                " and ec.fId=" + id +
                " and ec.source='124002'" +
                " order by ec.createTime", map).getDataRows();
        mainData.put("detailLength", detailList.size());
        mv.addObject("mainData", mainData);
        int first = 2;
        int pageSize = 3;
        mv.addObject("detailList1", detailList.subList(0, first));
        int pages = (detailList.size() - first) / pageSize;
        if (pages * pageSize < (detailList.size() - first)) pages++;
        List detailPages = new ArrayList();
        for (int i = 0; i < pages; i++) {
            Map maptemp = new HashMap();
            int start = first - 1 + i * pageSize;
            int end = first - 1 + (i + 1) * pageSize;
            if (end > detailList.size() - 1)
                end = detailList.size() - 1;
            maptemp.put("detailList", detailList.subList(start, end));
            detailPages.add(maptemp);
        }
        mv.addObject("detailPages", detailPages);
        mv.addObject("printTime", SqlTool.getNowTimeString2());
        return mv;
    }
}

